#!/bin/sh
## chang here
SERVICE_DIR=/opt/sca-eureka
SERVICE_NAME=sca-eureka
SPRING_PROFILES_ACTIVE=eureka01
## java env
export JAVA_HOME=/root/jdk1.8.0_181
export JRE_HOME=$JAVA_HOME/jre

case "$1" in
start)
	procedure=`ps -ef | grep -w "$SERVICE_NAME" | grep -w "java" | grep -v "grep" | awk '{print $2}'`
	if [ "$procedure" = "" ];
	then
		echo "start ..."
		if [ "$2" != "" ];
		then
			SPRING_PROFILES_ACTIVE=$2
		fi
		echo "spring.profiles.active=$SPRING_PROFILES_ACTIVE"
		exec nohup $JRE_HOME/bin/java -Xms128m -Xmx512m -jar $SERVICE_DIR/$SERVICE_NAME\.jar --spring.profiles.active=$SPRING_PROFILES_ACTIVE > $SERVICE_DIR/$SERVICE_NAME.log 2>&1 &
		procedure=`ps -ef | grep -w "$SERVICE_NAME" | grep -w "java" | grep -v "grep" | awk '{print $2}'`
		if [ "$procedure" = "" ];
		then
			echo "$SERVICE_NAME start failed"
		else
			echo "$SERVICE_NAME start success"
		fi
	else
		echo "$SERVICE_NAME has running"
	fi
;;
stop)
	procedure=`ps -ef | grep -w "$SERVICE_NAME" | grep -w "java" | grep -v "grep" | awk '{print $2}'`
	if [ "$procedure" = "" ];
	then
		echo "$SERVICE_NAME is not running"
	else
		kill -9 $procedure
		sleep 2
		procedure=`ps -ef | grep -w "$SERVICE_NAME" | grep -w "java" | grep -v "grep" | awk '{print $2}'`
		if [ "$argprocedure" = "" ];
		then
			echo "$SERVICE_NAME stop success"
		else
			echo "$SERVICE_NAME stop error"
		fi
	fi
;;
*)
	echo "usage: $0 [start|stop] [dev|test|prod]"
;;
esac